/**
 * 封装axios
 */
import axios from "axios";
import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);

var toast;

var instance = axios.create({
  baseURL: "http://shop.bufantec.com/bufan",
  timeout: 5000,
});

// 设置拦截器
instance.interceptors.request.use(
  function (config) {
    toast = Toast.loading({
      message: "加载中...",
      forbidClick: true,
    });
    // 请求方式是get 时 数据需要放到params中
    if (config.method.toLowerCase() == "get") {
      config.params = config.data;
    }
    console.log("config: ", config);
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  function (config) {
    toast.clear();
    return config;
  },
  function (error) {
    toast.clear();
    return Promise.reject(error);
  }
);

export default instance;
